package cn.yunhe.insurance.controller;


import cn.yunhe.insurance.entity.Cars;
import cn.yunhe.insurance.entity.Emp;
import cn.yunhe.insurance.entity.User;
import cn.yunhe.insurance.service.CarsService;
import cn.yunhe.insurance.util.DateUtil;
import cn.yunhe.insurance.util.Result;
import cn.yunhe.insurance.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 陈晓萌
 * 2018/9/17 17:25
 */
@RestController
@RequestMapping("/cars")
public class CarsController {

    @Resource
    private CarsService carsService;

    Result result = new Result();

    /**
     * 跳转分页查询页面
     *
     * @return
     */
    @RequestMapping("/find")
    public ModelAndView find() {
        ModelAndView mv = new ModelAndView("cars_list");
        return mv;
    }

    /**
     * 查看车辆信息
     *
     * @param pageSize
     * @param pageNum
     * @param licenseNum
     * @return
     */
    @RequestMapping("/findLikeCars")
    public Map findLikeCars(int pageSize, int pageNum, String licenseNum) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("licenseNum", licenseNum);
        Page page = carsService.findLikeCars(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;
    }

    /**
     * 跳转添加车辆信息页面
     *
     * @return
     */
    @RequestMapping("/addFirst")
    public ModelAndView addFirst() {
        ModelAndView mv = new ModelAndView("cars_add");
        return mv;
    }

    /**
     * 身份证号查询用户是否存在
     *
     * @param userCardId
     * @return
     */
    @RequestMapping("/getUserByCardId")
    public List<User> getUserByCardId(String userCardId) {
        User user = carsService.getUserByCardId(userCardId);
        List<User> userList = new ArrayList<>();
        userList.add(user);
        return userList;
    }

    /**
     * 添加车辆信息
     *
     * @param file
     * @param cars
     * @param session
     * @return
     */


    @RequestMapping(value = "/addCars", method = RequestMethod.POST)
    public ModelAndView addCars(@RequestParam MultipartFile file, Cars cars, HttpSession session) {
        String fileName=fileUpLoad(file);
        Emp emp = (Emp) session.getAttribute("emp");
        file.getContentType();
        cars.setCarUuid(UUIDGenerator.getUUID());
        cars.setEvaluator(emp.getEmpName());
        cars.setPic(fileName);
        cars.setAddTime(DateUtil.date2StringDateTime(new Date()));
        String UserId = cars.getUser().getSuId();
        cars.getUser().setSuId(UserId);
        System.out.println(cars.getUser().getSuId() + "添加的用户的id");
        carsService.addCars(cars);
        return new ModelAndView("cars_list");
    }

    /**
     * 查询车牌是否存在
     *
     * @param licenseNum
     * @return
     */
    @RequestMapping("/getCarById")
    public boolean getCarById(String licenseNum) {
        Cars carslicensenum = carsService.getCarById(licenseNum);
        if (carslicensenum != null) {
            return true;
        }
        return false;
    }

    /**
     * 删除车辆信息
     *
     * @param cars
     * @return
     */
    @RequestMapping("/delCars")
    public boolean delCars(Cars cars) {
        return carsService.delCars(cars);
    }

    /**
     * 根据uuid查询车辆信息（修改前查询）
     *
     * @return
     */
    @RequestMapping("/findCarsByUuid")
    public ModelAndView findCarsByUuid(String carUuid) {
        ModelAndView mv = new ModelAndView("cars_update");
        Cars cars = carsService.findCarsByUuid(carUuid);
        mv.addObject("cars", cars);
        return mv;
    }

    /**
     * 修改车辆信息
     *
     * @return
     */
    @RequestMapping("/updateCars")
    public ModelAndView updateCars(Cars cars) {
        ModelAndView mv = new ModelAndView("cars_list");
        carsService.updateCars(cars);
        return mv;
    }

    /**
     * 获取车辆ID
     *
     * @param cars
     * @return
     */
    @RequestMapping("/toCarId")
    public ModelAndView byCarId(Cars cars) {
        Cars c = carsService.getCarsById(cars);
        Map map = new HashMap();
        map.put("data", c);
        return new ModelAndView("cars_update", map);
    }

    /**
     * 修改车辆信息
     *
     * @param cars
     * @return
     */
    @RequestMapping("/cars_update")
    public Result editCars(@RequestParam MultipartFile file, Cars cars, HttpSession session,String oldPicName) {
        Emp emp = (Emp) session.getAttribute("emp");
        cars.setEvaluator(emp.getEmpName());
        cars.setAddTime(DateUtil.date2StringDateTime(new Date()));
        if (!file.isEmpty()) {

            String filePath="/usr/uploadFiles/";


            File oldFile=new File(filePath+File.separator+oldPicName);
            oldFile.delete();
            String fileName=fileUpLoad(file);
            cars.setPic(fileName);
        }
        carsService.upCarsMessage(cars);
        return result;
    }

    @RequestMapping("/update_cars")
    public Result updateCarStatus(Cars cars) {
        carsService.upCarsStatus(cars);
        return new Result();
    }

    public CarsService getCarsService() {
        return carsService;
    }

    public void setCarsService(CarsService carsService) {
        this.carsService = carsService;
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    public String fileUpLoad(MultipartFile file) {

        String filePath = "/usr/uploadFiles/";


        String fileName = "car"+System.currentTimeMillis()+file.getOriginalFilename();
        if (file != null) {
            File dest = new File(filePath + File.separator + fileName);
            if (!dest.getParentFile().isDirectory()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }
}